package cn.szzsi._admin;

import cn.szzsi.core.kit.UtilDate;
import cn.szzsi.service.DividendService;
import cn.szzsi.common.bean.dto.Msg;
import cn.szzsi.common.excel.PoiRender;
import cn.szzsi.common.kit.CommonUtil;

import cn.szzsi.common.model.CustomerDividend;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.render.Render;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * Created by zhengy on 2017/2/11.
 */
public class DividendController extends BaseAdminController<CustomerDividend>{
    private String shopId;
    public void list(){
        setAttr("yesterday", UtilDate.timestampToDateStr(UtilDate.getYestdayStartTime()));
        render(CommonUtil.TEMPLATE_PATH+"/business/dividend/index.html");
    }

    public void ajaxlist(){
        shopId=getAttr("shopId");
        Integer dataLevel=getParaToInt("type",1);//几级分红(默认1级分红)
        String profitDate=getPara("profitDate", UtilDate.timestampToDateStr(UtilDate.getYestdayStartTime())); //默认昨天凌晨时间
        Map<String,Object> result = DividendService.ds.getDividendList(dataLevel,shopId,profitDate);
        renderJson(result);
    }

    public void createprofit(){
        shopId=getAttr("shopId");
        String profitDate=getPara("createDate");//分红日期
        int dataLevel=getParaToInt("dataLevel");//几级分红
        setAttr("dataLevel",dataLevel);

        Long todayStartTime= UtilDate.getTodayStartTime();//今天凌晨的时间戳
        try {
            if(UtilDate.strToTimestamp(profitDate)>=todayStartTime){
                renderJson(Msg.fail("只能生成昨天或者昨天之前的分红金额!",""));
                return;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //判断所选分红日期是否存在
        if(CustomerDividend.dao.isExit(dataLevel,profitDate,shopId).size()>1){
            renderJson(Msg.fail("所选日期已生成分红金额!",""));
            return;
        }
        DividendService.ds.createProfit(shopId,dataLevel,profitDate);//生成分红金额
        renderJson(Msg.SUCCESS);

    }

    //导出分红金额
    public void exportprofit(){
        shopId=getAttr("shopId");
        String profitDate=getPara("profitDate");
        Integer dataLevel=getParaToInt("dataLevel");
        List<Record> list = Db.find("select m.code,m.username,m.phone,b.rewardnum,b.profit,(case when b.status=1 then '已分配' else '未分配' end)status,b.createDate  from mall_member m right join mall_customer_dividend b on m.id=b.mid where b.dataLevel=? and b.profitDate=? and b.uid=?",dataLevel,profitDate,shopId);
        String[] header={"会员ID","会员昵称","联系方式","直荐会员数","分红金额","分红状态","日期"};
        String[] columns={"code","username","phone","rewardnum","profit","status","createDate"};
        Render poirender = PoiRender.me(list).fileName("profit("+profitDate+").xls").headers(header).sheetName("辅导津贴").columns(columns);
        render(poirender);
    }

    //给会员分红金额
    public void distribution(){
        shopId=getAttr("shopId");
        String profitDate=getPara("profitDate");
        Integer dataLevel=getParaToInt("dataLevel");
       // CustomerDividend.dao.findById(id).set("status",1).update();
        DividendService.ds.distribution(dataLevel,profitDate,shopId);

        renderJson(Msg.SUCCESS);
    }
}
